﻿using System;
using System.Collections.Generic;
using System.Linq;

using Microsoft.TeamFoundation.WorkItemTracking.Client;


namespace Qetza.TeamFoundation.WorkItemQuery.Admin
{
    /// <summary>
    /// Command to list the work item queries.
    /// </summary>
    public class CommandListWiq : ICommand
    {
        #region ICommand Members

        /// <inheritdoc />
        public string Name
        {
            get { return "list"; }
        }

        /// <inheritdoc />
        public string HelpText
        {
            get { return ResourceStrings.ListWiqHelpText; }
        }

        /// <inheritdoc />
        public IEnumerable<ParameterInfo> AllowedParameters
        {
            get
            {
                return new[] {
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Server, 
                        ArgumentName = ResourceStrings.ServerArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ServerHelpText
                        },
                    new ParameterInfo() { 
                        Name = CoreArgumentNames.Project, 
                        ArgumentName = ResourceStrings.ProjectArgument, 
                        IsRequired = true, 
                        HelpText = ResourceStrings.ProjectHelpText
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.Private,
                        IsFlag = true,
                        HelpText = ResourceStrings.PrivateFlagHelpText,
                        IncompatibleArguments = new [] { CoreArgumentNames.Public }
                        }, 
                    new ParameterInfo() {
                        Name = CoreArgumentNames.Public,
                        IsFlag = true,
                        HelpText = ResourceStrings.PublicFlagHelpText
                        }
                    };
            }
        }

        /// <inheritdoc />
        public void Execute(IDictionary<string, string> parameters)
        {
            // filter queries (all/public/private)
            var storedQueries = TfsUtility
                .GetWorkItemStore(parameters[CoreArgumentNames.Server], parameters.ContainsKey(CoreArgumentNames.NoPrompt))
                .Projects[parameters[CoreArgumentNames.Project]]
                .StoredQueries
                .Cast<StoredQuery>();

            if (parameters.ContainsKey(CoreArgumentNames.Private))
            {
                storedQueries = storedQueries.Where(q => q.QueryScope == QueryScope.Private);
            }

            if (parameters.ContainsKey(CoreArgumentNames.Public))
            {
                storedQueries = storedQueries.Where(q => q.QueryScope == QueryScope.Public);
            }

            // display queries
            foreach (var query in storedQueries)
            {
                Console.WriteLine(query.Name);
            }
        }

        /// <inheritdoc />
        public void ValidateParameters(IDictionary<string, string> parameters)
        {
            return;
        }

        #endregion
    }
}
